/**
 * 記錄登陸會員信息
 */
function loginUser() {
	this.username = '';
	this.password = '';
	this.user = null;	
}
/**
 * 用戶登陸類
 * @param	r_user	用戶對象
 * @param	r_name	用戶帳號
 * @param	r_pass	用戶密碼
 */
function login(r_id, r_key, r_chan) {				
	var _sql = "SELECT * FROM texas_login_key WHERE login_key='" + r_key +"' AND status='0'";
	var _data = __conn.selectOne(_sql);	
	if ( _data == null ) return false;
	if ( parseInt(_data.getItem("user_id")) != parseInt(r_id) ) return false;
	_sql = "SELECT * FROM texas_member WHERE member_id='" + r_id +"'";
	_res = __conn.selectOne(_sql);
	if (_res == null) return false;
	var _zone = _server.getCurrentZone();
	var _old_user = _zone.getUserByName(r_id);
	if (_old_user) {
		_server.logoutUser(_old_user);
	}
	var _result = _server.loginUser(r_id, '', r_chan, true);	 //執行登錄
	if ( _result.success ) {
		
		/* 修改数据库玩家状态 */
		_sql = "UPDATE texas_member SET gold = gold+%d, chip = 0, last_login_time='%d', stauts='online'  WHERE member_id='%d' ";
		var _arg = [_res.getItem('chip'), getTime(), r_id]		
		__conn.execute(_sql,_arg);	
		/* 修改key状态 */
		//_sql = "UPDATE texas_login_key SET status='1' WHERE login_key='"+r_key+"'";
		//__conn.execute(_sql);
		
		var _user = _server.getUserByChannel(r_chan);		
		/* 設置玩家變量 */
		var _user_param = { }; 
		_user_param["userId"] = parseInt(_res.getItem("member_id"));
		_user_param["gold"]   = parseInt(_res.getItem("gold")) + parseInt(_res.getItem('chip')); 	//玩家总点数
		_user_param["chip"]   = 0;		//初始化筹码
		_user_param["level"]  = parseInt(_res.getItem("level"));
		_user_param["label"]  = parseInt(_res.getItem("label_value"));				
		_user_param["name"]   = String(_res.getItem("nick_name"));
		_user_param["face"]   = String(_res.getItem("user_photo"));				
		_user_param["friends"]= String(_res.getItem("friends"));				
		_user_param["state"]  = 0;				
		//_user_param["bank"]   = 0;									
		__userVars.setVar(_user, _user_param);  //設置玩家變量
		var _param = { };
		_param._cmd = 'loginOk';
		var _data = { };
		_data.user = __userVars.getUserVars(_user);
		_data.hall = __roomEvent.getHallList();
		//_data.room = __roomEvent.getRoomList();
		_param.dataObj = _data;
		
		/* 記錄登錄日志 */
		_sql = "INSERT INTO texas_log (id,member_id,log_info,log_info_extend,log_type,log_time) VALUES (NULL,'%d','%s','%s','%s','%d')";
		var _arg = [r_id,_user.getIpAddress(),'1','login',getTime()];
		__conn.execute(_sql,_arg);
		
		return _param;
	}else {
		/* 記錄登錄日志 */
		_sql = "INSERT INTO texas_log (id,member_id,log_info,log_info_extend,log_type,log_time) VALUES (NULL,'%d','%s','%s','%s','%d')";
		var _arg = [r_id,"loginFail",'0','login',getTime()];
		__conn.execute(_sql,_arg);
	}
	return false;	
}

